<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="user" labelWidth="80px">
				<uni-forms-item label="学号" name="sno" required>
					<uni-easyinput v-model="user.sno" placeholder="请输入学号" disabled="disabled" />
				</uni-forms-item>

				<uni-forms-item label="姓名" name="name" required>
					<uni-easyinput v-model="user.name" placeholder="请输入姓名" disabled="disabled" />
				</uni-forms-item>

				<uni-forms-item label="性别" name="gender" required>
					<uni-data-checkbox v-model="user.gender" :localdata="sexs" />
				</uni-forms-item>

				<uni-forms-item label="年级" name="schoolYear" required>
					<uni-easyinput v-model="user.schoolYear" placeholder="请输入年级" disabled="disabled" />
				</uni-forms-item>

				<uni-forms-item label="班级" name="gradeName" required>
					<uni-easyinput v-model="user.gradeName" placeholder="请输入班级" disabled="disabled" />
				</uni-forms-item>

				<uni-forms-item label="电话" name="phone" required>
					<uni-easyinput v-model="user.phone" placeholder="请输入手机号码" />
				</uni-forms-item>

				<uni-forms-item label="邮箱" name="mail" required>
					<uni-easyinput v-model="user.mail" placeholder="请输入邮箱" />
				</uni-forms-item>

				<uni-forms-item label="家庭住址" name="location" required>
					<uni-easyinput type="textarea" v-model="user.location" placeholder="请输入家庭住址"></uni-easyinput>
				</uni-forms-item>

			</uni-forms>
			<button type="primary" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		getUserProfile
	} from "@/api/system/user"
	import {
		updateUserProfile
	} from "@/api/system/user"

	export default {
		data() {
			return {
				user: {
					phone: "",
					email: "",
					gender: ""
				},
				sexs: [{
					text: '男',
					value: 1
				}, {
					text: '女',
					value: 0
				}],
				rules: {
					phone: {
						rules: [{
							required: true,
							errorMessage: '手机号码不能为空'
						}, {
							pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
							errorMessage: '请输入正确的手机号码'
						}]
					},
					mail: {
						rules: [{
							required: true,
							errorMessage: '邮箱地址不能为空'
						}, {
							format: 'email',
							errorMessage: '请输入正确的邮箱地址'
						}]
					},
					gender: {
						rules: [{
							required: true,
							errorMessage: '性别不能为空'
						}]
					},
					location: {
						rules: [{
							required: true,
							errorMessage: '家庭地址不能为空'
						}]
					}
				}
			}
		},
		onLoad() {
			this.getUser()
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			getUser() {
				getUserProfile().then(response => {
					// console.log(response)
					this.user = response.data
				})
			},
			submit(ref) {
				this.$refs.form.validate().then(res => {
					updateUserProfile(this.user).then(response => {
						this.$modal.msgSuccess("修改个人信息成功")
						setTimeout(() => {
						  uni.reLaunch({
						    url: '/pages/mine/index'
						  });
						}, 3000);
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>